package xyz.penghaonan.adtools.adview;

import android.app.Activity;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.penghaonan.appframework.utils.DebugUtils;

import xyz.penghaonan.adtools.ADLog;
import xyz.penghaonan.adtools.ADTools;

public class FullScreenAd {

    private Callback callback;
    private ViewGroup container;

    public interface Callback {
        void onShow();

        void onShowFailed(String msg);

        void onClose();
    }

    public void startLoad(@NonNull ViewGroup container, @NonNull String ad_id, @NonNull Callback callback) {
        if (!ADTools.isEnable()) {
            return;
        }
        this.container = container;
        DebugUtils.assertTrue(container.getContext() instanceof Activity, "Container context is not Activity");
        this.callback = callback;
        ADLog.i("startLoad");
        AdRequest adRequest = new AdRequest.Builder().build();
        InterstitialAd.load(container.getContext(), ad_id, adRequest,
                new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        ADLog.i("onAdLoaded");
                        interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                            @Override
                            public void onAdDismissedFullScreenContent() {
                                // Called when fullscreen content is dismissed.
                                ADLog.i("onAdDismissedFullScreenContent");
                                callback.onClose();
                            }

                            @Override
                            public void onAdFailedToShowFullScreenContent(AdError adError) {
                                // Called when fullscreen content failed to show.
                                ADLog.i("onAdFailedToShowFullScreenContent : " + adError.getMessage());
                                callback.onShowFailed(adError.getMessage());
                            }

                            @Override
                            public void onAdShowedFullScreenContent() {
                                // Called when fullscreen content is shown.
                                // Make sure to set your reference to null so you don't
                                // show it a second time.
                                ADLog.i("onAdShowedFullScreenContent");
                                callback.onShow();
                            }
                        });
                        interstitialAd.show((Activity) container.getContext());
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        ADLog.i("onAdFailedToLoad : " + loadAdError.getMessage());
                        callback.onShowFailed(loadAdError.getMessage());
                    }
                });
    }
}
